package cn.yangliu.nacos.comm.tools;

import java.util.Objects;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * The type of nacos.
 *
 * @author 问道于盲
 * @date 2020 -01-01
 */
public class SecurityUtils {

    /**
     * Instantiates a new Security utils.
     * forbid other class to create a new instance.
     * only itself can create a new instance.
     */
    private SecurityUtils() {

    }

    /**
     * current class method for getter.
     * Gets current user.
     *
     * @param <T> the type parameter
     * @return the current user
     */
    public static <T> T getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (Objects.isNull(authentication)) {
            return null;
        }
        return (T) authentication.getPrincipal();
    }
}
